package com.shop2cn.iapi.prodstock.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.activitymgt.parameter.AddactivityBean;
import com.shop2cn.iapi.prodstock.parameter.OrderReturnCatalogDto;
import com.shop2cn.iapi.prodstock.parameter.ReturnStockForDeliveryBean;
import com.shop2cn.iapi.prodstock.service.ReturnStockForDeliveryCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AgentProductCallServiceV2;
import com.ymttest.business.service.Shop2cnStockCallService;
import com.ymttest.database.model.GodProductActivityStock;
import com.ymttest.database.sqlwapper.GodProductActivityStockWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;
/**
*取消发货返还商品库存Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ReturnStockForDelivery{
	private static ReturnStockForDeliveryBean returnstockfordeliveryBean;
	private static ReturnStockForDeliveryCall returnstockfordeliveryCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("取消发货返还商品库存Test");
	}

	@Before
	public void caseUp() {
		returnstockfordeliveryBean=new ReturnStockForDeliveryBean();
		returnstockfordeliveryCall=new ReturnStockForDeliveryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(returnstockfordeliveryCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	
	
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";

	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnStockForDelivery_001() {
		Logger.start(true,"30真实，0虚拟，发货十件真实，之后发货取消，最终30真实，0虚拟");
		try {
			
			//30真实，0虚拟，发货十件真实，之后发货取消，最终30真实，0虚拟
			//新建商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(0);
			addAgentProductBean.getCatalogList().get(0).setStock(30);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			//发货虚拟十件
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			long OrderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId,productId,10);
			productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			returnstockfordeliveryBean.setOrderId(OrderId);
			returnstockfordeliveryBean.setBizNo(TestDataManager.getRandomNum(10));
			OrderReturnCatalogDto orderReturnCatalogDto = new OrderReturnCatalogDto();
			long RefundBillNo=Long.parseLong(TestDataManager.getRandomNum(10));
			orderReturnCatalogDto.setRefundBillNo(RefundBillNo);
			orderReturnCatalogDto.setOrderId(OrderId);
			orderReturnCatalogDto.setActivityId(0);
			orderReturnCatalogDto.setBuyerId(AgentUsers.csId1);
			orderReturnCatalogDto.setCatalogId(catalogId);
			orderReturnCatalogDto.setCatalogType(0);
			orderReturnCatalogDto.setOrderIsPaid(false);
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setSellerId(rootUserId);
			orderReturnCatalogDto.setStockNum(10);
			returnstockfordeliveryBean.setOrderReturnCatalogDto(orderReturnCatalogDto);
			returnstockfordeliveryCall.setData(returnstockfordeliveryBean);
			returnstockfordeliveryCall.callService();		
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
			Logger.verifyEquals(0,productDetailsDto2.getCatalogList().get(0).getVirtualStock(),"验证虚拟库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnStockForDelivery_002() {
		Logger.start(true,"活动和活动开始售卖 ");
		try {
			/***
			 * 商品A  真实库存 100   虚拟库存100
			 *活动时间8月1日0点-24点，活动库存100件
			 *当前日期7月27日
			 *7月27日-8月1日0点 商品正常售卖（非活动）50件，扣的库存全为虚拟库存，且不能用被活动锁定的真实库存来核销待核销的订单
			 *8月1日活动开始，活动卖出商品30件，扣的是真实库存，
			 *活动结束，还真实库存70件，并同时核销前面的虚拟库存订单
			 */
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(100);
			addAgentProductBean.getCatalogList().get(0).setStock(100);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//创建活动，锁定100库存--剩余100虚拟
			AddactivityBean initProductaddactivity = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,1,100, false);
			Integer activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(100, productDetailsDto.getCatalogList().get(0).getVirtualStock(), "验证虚拟库存");
			Logger.verifyEquals(100, productDetailsDto.getCatalogList().get(0).getStock(), "验证总库存");
			//正常售卖50件虚拟库存
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,catalogId,productId,50);
			long OrderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId,productId,50,order);
			productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(50, productDetailsDto.getCatalogList().get(0).getVirtualStock(), "验证虚拟库存");
			Logger.verifyEquals(50, productDetailsDto.getCatalogList().get(0).getStock(), "验证总库存");
			//售卖30件活动库存
			long activityOrderId = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,activityId,catalogId,productId,30);
			productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(50, productDetailsDto.getCatalogList().get(0).getStock(), "验证总库存");
			Logger.verifyEquals(50, productDetailsDto.getCatalogList().get(0).getVirtualStock(), "验证虚拟库存");
			GodProductActivityStock selectByProductId = new GodProductActivityStockWapper().selectByProductIdAndCatalogId(productId, catalogId);
//			Logger.verifyEquals(50, selectByProductId.getiStockNum(), "验证活动库存");
			
			//活动结束，自动核销虚拟库存
			Shop2cnStockCallService.ReturnProductActivityStock(activityId, initProductaddactivity);
			
			productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(50, productDetailsDto.getCatalogList().get(0).getVirtualStock(), "验证虚拟库存");
//			Logger.verifyEquals(20, productDetailsDto.getCatalogList().get(0).getStock(), "验证总库存");
			selectByProductId = new GodProductActivityStockWapper().selectByProductIdAndCatalogId(productId, catalogId);
			Logger.verifyEquals(0, selectByProductId.getiStockNum(), "验证活动库存");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnStockForDelivery_003() {
		Logger.start(true,"70不锁定库存的活动，发货十件，之后发货取消，最终70活动");
		try {
			//活动20件，下单十件，之后订单取消，返还
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddactivityBean initProductaddactivity = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,0,null, false);
			Integer activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			//发货出库十件
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
//			long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,activityId,catalogId,productId,10);
			long OrderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId,productId,10);
			productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			GodProductActivityStock selectByProductId = new GodProductActivityStockWapper().selectByProductIdAndCatalogId(productId, productDetailsDto.getCatalogList().get(0).getCatalogId());
			
			returnstockfordeliveryBean.setOrderId(OrderId);
			returnstockfordeliveryBean.setBizNo(TestDataManager.getRandomNum(10));
			OrderReturnCatalogDto orderReturnCatalogDto = new OrderReturnCatalogDto();
			long RefundBillNo=Long.parseLong(TestDataManager.getRandomNum(10));
			orderReturnCatalogDto.setRefundBillNo(RefundBillNo);
			orderReturnCatalogDto.setOrderId(OrderId);
			orderReturnCatalogDto.setActivityId(activityId);
			orderReturnCatalogDto.setBuyerId(AgentUsers.csId1);
			orderReturnCatalogDto.setCatalogId(catalogId);
			orderReturnCatalogDto.setCatalogType(0);
			orderReturnCatalogDto.setOrderIsPaid(false);
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setSellerId(rootUserId);
			orderReturnCatalogDto.setStockNum(10);
			returnstockfordeliveryBean.setOrderReturnCatalogDto(orderReturnCatalogDto);
			returnstockfordeliveryCall.setData(returnstockfordeliveryBean);
			returnstockfordeliveryCall.callService();
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getVirtualStock(),"验证虚拟库存");
			
			GodProductActivityStock selectByProductId1 = new GodProductActivityStockWapper().selectByProductIdAndCatalogId(productId, productDetailsDto.getCatalogList().get(0).getCatalogId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}